package com.ruoyi.zyx.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zyx.domain.ZyxMemberDevice;
import com.ruoyi.zyx.mapper.ZyxMemberDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zyx.mapper.ZyxMemberMapper;
import com.ruoyi.zyx.domain.ZyxMember;
import com.ruoyi.zyx.service.IZyxMemberService;

/**
 * 成员基础信Service业务层处理
 *
 * @author lhs
 * @date 2021-05-25
 */
@Service
public class ZyxMemberServiceImpl implements IZyxMemberService
{
    @Autowired
    private ZyxMemberMapper zyxMemberMapper;
    @Autowired
    private ZyxMemberDeviceMapper zyxMemberDeviceMapper;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询成员基础信
     *
     * @param id 成员基础信ID
     * @return 成员基础信
     */
    @Override
    public ZyxMember selectZyxMemberById(Long id)
    {
        return zyxMemberMapper.selectZyxMemberById(id);
    }

    @Override
    public ZyxMember selectZyxMemberByUserId(Long userid) {
        return zyxMemberMapper.selectZyxMemberByUserId(userid);
    }

    /**
     * 查询成员基础信列表
     *
     * @param zyxMember 成员基础信
     * @return 成员基础信
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "")
    public List<ZyxMember> selectZyxMemberList(ZyxMember zyxMember)
    {
        return zyxMemberMapper.selectZyxMemberList(zyxMember);
    }

    @DataScope(deptAlias = "d", userAlias = "")
    public List<ZyxMember> zyxMemberCreditlist(ZyxMember zyxMember) {
        return zyxMemberMapper.zyxMemberCreditlist(zyxMember);
    }

    /**
     * 新增成员基础信
     *
     * @param zyxMember 成员基础信
     * @return 结果
     */
    @Override
    public int insertZyxMember(ZyxMember zyxMember)
    {
        zyxMember.setCreateTime(DateUtils.getNowDate());
         //开通成员账号
        SysUser sysUser=new SysUser();
        sysUser.setNickName(zyxMember.getName());
        sysUser.setUserName(zyxMember.getMobile());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setStatus("0");
        Long[] str ={102l};
        sysUser.setRoleIds(str);//工程师
        sysUser.setDeptId(zyxMember.getDepartid());
        sysUserService.insertUser(sysUser);

        zyxMember.setUserid(sysUser.getUserId());
        int rows=zyxMemberMapper.insertZyxMember(zyxMember);
        return rows;
    }

    /**
     * 修改成员基础信
     *
     * @param zyxMember 成员基础信
     * @return 结果
     */
    @Override
    public int updateZyxMember(ZyxMember zyxMember)
    {
        zyxMember.setUpdateTime(DateUtils.getNowDate());
        Integer rows=zyxMemberMapper.updateZyxMember(zyxMember);
        // 删除人员与设备关联
//        zyxMemberDeviceMapper.deleteZyxMemberDeviceByMemberId(zyxMember.getId());
        return rows;
    }

    /**
     * 批量删除成员基础信
     *
     * @param ids 需要删除的成员基础信ID
     * @return 结果
     */
    @Override
    public int deleteZyxMemberByIds(Long[] ids)
    {
        for (Long id : ids) {
            ZyxMember zyxMember=zyxMemberMapper.selectZyxMemberById(id);
            sysUserService.deleteUserById(zyxMember.getUserid());
        }
        return zyxMemberMapper.deleteZyxMemberByIds(ids);
    }

    /**
     * 删除成员基础信信息
     *
     * @param id 成员基础信ID
     * @return 结果
     */
    @Override
    public int deleteZyxMemberById(Long id)
    {
        return zyxMemberMapper.deleteZyxMemberById(id);
    }
}
